//
//  UIView+Load.m
//  LenovoClub
//
//  Created by LQiyue on 2017/10/13.
//  Copyright © 2017年 qiyue. All rights reserved.
//

#import "UIView+Load.h"
#import "UIView+Category.h"

static char EmptyViewKey;
NSString * const kErrorNetDefaultMessage  =      @"网络连接异常，请稍后重试";

@implementation UIView (Load)

- (void)setEmptyView:(LoadEmptyView *)emptyView
{
    [self willChangeValueForKey:@"EmptyViewKey"];
    objc_setAssociatedObject(self, &EmptyViewKey,
                             emptyView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"EmptyViewKey"];
}


- (LoadEmptyView *)emptyView {
    return objc_getAssociatedObject(self, &EmptyViewKey);
}

- (void)configWithEmptyType:(LoadEmptyType)emptyType emptyBtnBlock:(void(^)(id sender))block
{
    if (emptyType == 0) {
        self.emptyView.hidden = YES;
        [self.emptyView removeFromSuperview];
        
    } else {
        if (!self.emptyView) {
            self.emptyView = [[LoadEmptyView alloc] initWithFrame:self.bounds];
        }
        self.emptyView.hidden = NO;
        [self.blankContainer addSubview:self.emptyView];
        [self.emptyView configWithEmptyType:emptyType emptyBtnBlock:block];
    }
}
- (void)configWithEmptyType:(LoadEmptyType)emptyType hasError:(BOOL)hasError imageName:(NSString *)imageName tip:(NSString *)tip btnTitle:(NSString *)btnTitle emptyBtnBlock:(void(^)(id sender))block {
    if (emptyType == 0) {
        self.emptyView.hidden = YES;
        [self.emptyView removeFromSuperview];
       
    } else {
        if (!self.emptyView) {
            self.emptyView = [[LoadEmptyView alloc] initWithFrame:self.bounds];
        }
        self.emptyView.hidden = NO;
        [self.blankContainer addSubview:self.emptyView];
        [self.emptyView configWithEmptyType:emptyType hasError:hasError imageName:imageName tip:tip btnTitle:btnTitle emptyBtnBlock:block];
    }
        
    
}

#pragma mark - new method start

- (void)configWithEmptyType:(LoadEmptyType)emptyType hasError:(BOOL)hasError imageName:(NSString *)imageName tip:(NSString *)tip btnTitle:(NSString *)btnTitle bgColorStr:(NSString *)bgColorStr emptyBtnBlock:(void(^)(id sender))block {
    if (emptyType == 0) {
        self.emptyView.hidden = YES;
        [self.emptyView removeFromSuperview];
       
    } else {
        if (!self.emptyView) {
            self.emptyView = [[LoadEmptyView alloc] initWithFrame:self.bounds];
        }
        self.emptyView.hidden = NO;
        self.emptyView.backgroundColor = HexColor(bgColorStr);
        [self.blankContainer addSubview:self.emptyView];
        [self.emptyView configWithEmptyType:emptyType hasError:hasError imageName:imageName tip:tip btnTitle:btnTitle emptyBtnBlock:block];
    }
        
    
}

- (void)configWithEmptyType:(LoadEmptyType)emptyType hasError:(BOOL)hasError imageName:(NSString *)imageName tip:(NSString *)tip btnTitle:(NSString *)btnTitle emptyViewTop:(CGFloat)top bgColorStr:(NSString *)bgColorStr emptyBtnBlock:(void(^)(id sender))block {
    if (emptyType == 0) {
        self.emptyView.hidden = YES;
        [self.emptyView removeFromSuperview];
       
    } else {
        if (!self.emptyView) {
            self.emptyView = [[LoadEmptyView alloc] initWithFrame:CGRectMake(0, top, self.width, self.height-top)];
        }
        self.emptyView.frame = CGRectMake(0, top, self.width, self.height-top);
        
        self.emptyView.hidden = NO;
        [self.blankContainer addSubview:self.emptyView];
        [self.emptyView configWithEmptyType:emptyType hasError:hasError imageName:imageName tip:tip btnTitle:btnTitle bgColorStr:bgColorStr emptyBtnBlock:block];
    }
}

#pragma mark - new method end

- (UIView *)blankContainer{
    UIView *blankContainer = self;
    for (UIView *aView in [self subviews]) {
        if ([aView isKindOfClass:[UITableView class]]) {
            if (aView.height > 0) {
                blankContainer = aView;
            }
        }
    }
    return blankContainer;
}
- (void)removeEmptyView
{
    if (self.emptyView != nil) {
        [self.emptyView removeEmptyView];
    }
}

@end




#define CART_EXTSTR (@"购物车竟然是空的") //
#define CART_TIPSTR (@"想要的，现在就要，别犹豫~")
#define CART_BTNSTR (@"去商城逛逛")

#define NET_ERROR_BTNSTR (@"重新加载")
#define kColor_BACKGROUND   [UIColor colorWithHexString:@"0xEDF1F2"]
#define kColor_Main_Pink [UIColor colorWithHexString:@"0xF4364C"] // 粉红 主副色调

#define kScaleW_iPhone6(_X_)  (_X_ * (kSCREEN_WIDTH/375))
#define kScaleH_iPhone6(_X_)  (_X_ * (kSCREEN_HEIGHT/667))

#define kColor_Main         [UIColor colorWithHexString:@"0xFF8200"]

@interface LoadEmptyView()
@property (nonatomic, strong) UIImageView   *emptyImageView;
@property (nonatomic, strong) UILabel       *tipLabel, *extLabel;
@property (nonatomic, strong) UIButton      *btn;
@end

@implementation LoadEmptyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kColor_BACKGROUND;
    }
    return self;
    
}

- (void)setEmptyColor:(UIColor *)color {
    UIColor *bgColor = color;
    self.backgroundColor = bgColor;
}

- (void)configWithEmptyType:(LoadEmptyType)emptyType emptyBtnBlock:(void(^)(id sender))block
{
    // 购物车空白页面
    [self addSubview:self.emptyImageView];
    [self addSubview:self.tipLabel];
    [self addSubview:self.extLabel];
    [self addSubview:self.btn];
    
    if (emptyType == LoadEmptyType_Cart) {
        [_emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(kScaleW_iPhone6(80));
        }];
        [_emptyImageView setImage:[UIImage imageNamed:@"empty_cart"]];
        [self.extLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self->_emptyImageView);
            make.top.equalTo(self.emptyImageView.mas_bottom).offset(22);
            make.width.mas_equalTo(kSCREEN_WIDTH);
            make.height.mas_equalTo(20);
        }];
        _extLabel.text = CART_EXTSTR;
        [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self->_emptyImageView);
            make.top.equalTo(self->_extLabel.mas_bottom).offset(8);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(kSCREEN_WIDTH);
        }];
        _tipLabel.text = CART_TIPSTR;
        [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self->_emptyImageView);
            make.top.equalTo(self->_tipLabel.mas_bottom).offset(20);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(102);
        }];
        [_btn setTitle:CART_BTNSTR forState:UIControlStateNormal];
        [_btn doBorderWidth:0.5 color:kColor_Main_Pink cornerRadius:2];
        [_btn setTitleColor:kColor_Main_Pink forState:UIControlStateNormal];
        [self.btn addTarget:self action:@selector(reloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        _reloadEmptyBtnBlock = block;
    }
    if (emptyType == LoadEmptyType_NetError) {
        if(@available(iOS 13.0,*)){
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDarkContent];
        }
        [self setEmptyColor:UIColor.whiteColor];
        [self.extLabel removeFromSuperview];
        [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self->_emptyImageView);
            make.top.equalTo(self->_tipLabel.mas_bottom).offset(20);
            make.height.mas_equalTo(34);
            make.width.mas_equalTo(100);
        }];
        [_btn setTitle:NET_ERROR_BTNSTR forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:12];
        _btn.backgroundColor = HexColor(@"FF2F2F");
        [_btn doBorderWidth:0 color:nil cornerRadius:18];
        [_btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [self.btn addTarget:self action:@selector(reloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        _reloadEmptyBtnBlock = block;
        [_emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).mas_offset(kScaleH_iPhone6(-90));
        }];
        [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self->_emptyImageView);
            make.top.equalTo(self->_emptyImageView.mas_bottom).offset(20);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(kSCREEN_WIDTH);
        }];
        _tipLabel.text = kErrorNetDefaultMessage;
        _tipLabel.textColor = HexColor(@"606060");
        _tipLabel.font = [UIFont systemFontOfSize:12];
        [_emptyImageView setImage:[UIImage imageNamed:@"empty_net"]];
    }
    if (emptyType == 0) {
        [self.extLabel removeFromSuperview];
        [self.emptyImageView removeFromSuperview];
        [self.tipLabel removeFromSuperview];
        [self.btn removeFromSuperview];
    }
}

- (void)configWithEmptyType:(LoadEmptyType)emptyType hasError:(BOOL)hasError imageName:(NSString *)imageName tip:(NSString *)tip btnTitle:(NSString *)btnTitle emptyBtnBlock:(void(^)(id sender))block
{
    [self addSubview:self.emptyImageView];
    [self addSubview:self.tipLabel];
    
    // 空白页面 有点击按钮的状态
    if ([btnTitle length] > 0) {
        [_emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_centerY).offset(-50);
        }];
        [self addSubview:self.btn];
        [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self->_emptyImageView.mas_bottom).offset(20);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(88);
        }];
        [_btn setTitle:btnTitle forState:UIControlStateNormal];
        [_btn doBorderWidth:0.5 color:kColor_Main cornerRadius:2];
        [_btn setTitleColor:kColor_Main forState:UIControlStateNormal];
        [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.centerX.equalTo(self);
            make.top.equalTo(self->_btn.mas_bottom).offset(16);
            make.height.mas_equalTo(20);
        }];
        
    } else {
        // 空白页面 没有点击按钮的状态
        _btn.hidden = nil;
        [_emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_centerY).offset(-20);
        }];
        [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.centerX.equalTo(self);
            make.top.equalTo(self->_emptyImageView.mas_bottom).offset(16);
            make.height.mas_equalTo(20);
        }];
    }
    switch (emptyType) {
        case 0:
        {
            [self.extLabel removeFromSuperview];
            [self.emptyImageView removeFromSuperview];
            [self.tipLabel removeFromSuperview];
        }
            break;
        case LoadEmptyType_NoData:
        {
            [_emptyImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", imageName]]];
            [_tipLabel setText:tip];
            if ([btnTitle length] > 0) {
                 [_btn addTarget:self action:@selector(reloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                _reloadEmptyBtnBlock = block;
            }
        }
            break;
        case LoadEmptyType_DataFailed:
            break;
        case LoadEmptyType_NoData_BGColor:
        {
            self.backgroundColor = [UIColor whiteColor];
            [_emptyImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", imageName]]];
            [_tipLabel setText:tip];
            if ([btnTitle length] > 0) {
                [_btn addTarget:self action:@selector(reloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                _reloadEmptyBtnBlock = block;
            }
        }
            break;
        default:
            break;
    }
}
- (void)reloadButtonClicked:(id)sender{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.reloadEmptyBtnBlock) {
            self.reloadEmptyBtnBlock(sender);
        }
    });
}
- (void)removeEmptyView
{
    self.hidden = YES;
    [self removeFromSuperview];
}

- (UIImageView *)emptyImageView
{
    if (!_emptyImageView) {
        _emptyImageView = [[UIImageView alloc] init];
        _emptyImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _emptyImageView;
}
- (UILabel *)tipLabel
{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = [UIFont systemFontOfSize:14];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.textColor = [UIColor colorWithHexString:@"0x808080" andAlpha:0.4];
    }
    return _tipLabel;
}
- (UILabel *)extLabel
{
    if (!_extLabel) {
        _extLabel = [[UILabel alloc] init];
        _extLabel.font = [UIFont systemFontOfSize:16];
        _extLabel.textAlignment = NSTextAlignmentCenter;
        _extLabel.textColor = [UIColor colorWithHexString:@"0x808080"];
    }
    return _extLabel;
}


- (UIButton *)btn
{
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_btn setTitleColor:kColor_Main forState:UIControlStateNormal];
    }
    return _btn;
}

@end
